package com.digiwin.dap.middleware.aspect;

import com.digiwin.dap.middleware.auth.AppAuthContext;
import com.digiwin.dap.middleware.auth.AppAuthContextHolder;
import com.digiwin.dap.middleware.entity.BaseEntity;
import com.digiwin.dap.middleware.util.HashUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 数据切片，在保存之前进行基础数据修改
 */
@Aspect
@Component
public class AutoSetEntityManagerFieldAspect {

    /**
     * 定义保存的切点
     */
    @Pointcut("execution(public * com.digiwin.dap.middleware.service.impl.BaseEntityManagerService+.create(..))")
    public void create(){}

    /**
     * 定义保存的切点
     */
    @Pointcut("execution(public * com.digiwin.dap.middleware.service.impl.BaseEntityManagerService+.update(..))")
    public void update(){}

    /**
     * 在保存之前对管理字段赋值
     */
    @Around("create()")
    public Object addManagerEntityPropertiesValue(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();

        AppAuthContext context = AppAuthContextHolder.getContext();
        long currentUserSid = 0;
        //如果登录成功,设置当前应用id以及用户sid
        if (context.getAuthoredUser()!=null){
            currentUserSid=context.getAuthoredUser().getSid();
        }

        long appSid = 0;
        if (context.getAuthoredSys()!=null){
            appSid =context.getAuthoredSys().getSid();
        }
        //设置管理字段值
        for (Object arg : args) {
            if (arg instanceof BaseEntity) {
                BaseEntity entity = (BaseEntity) arg;
                entity.setCreateDate(LocalDateTime.now());
                entity.setCreateBy(currentUserSid);
                entity.setCreateOrg(0L);
                entity.setCreateProvider(appSid);
                entity.setModifyDate(LocalDateTime.now());
                entity.setModifyBy(currentUserSid);
                entity.setModifyProvider(appSid);
                entity.setHash(HashUtils.hash(HashUtils.objectToString(entity)));
            }

        }
        return pjp.proceed();
    }

    /**
     * 在修改之前对管理字段进行修改
     */
    @Around("update()")
    public Object updateManagerEntityPropertiesValue(ProceedingJoinPoint pjp) throws Throwable {

        Object[] args = pjp.getArgs();
        AppAuthContext context = AppAuthContextHolder.getContext();
        long currentUserSid = 0;
        //如果登录成功,设置当前应用id以及用户sid
        if (context.getAuthoredUser()!=null){
            currentUserSid=context.getAuthoredUser().getSid();
        }
        long appSid = 0;
        if (context.getAuthoredSys()!=null){
            appSid =context.getAuthoredSys().getSid();
        }
        //如果数据已经被删除或者被编辑，不允许继续处理
         for (Object arg : args) {
            if (arg instanceof BaseEntity) {
                BaseEntity entity = (BaseEntity) arg;
                entity.setModifyDate(LocalDateTime.now());
                entity.setModifyBy(currentUserSid);
                entity.setModifyProvider(appSid);
            }
        }
        return pjp.proceed();
    }
}
